using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GraphAlgsPresentation.Core
{
    /// <summary>
    /// Represents a user control of GraphPlotter
    /// </summary>
    public partial class GraphPlotter : UserControl
    {
        private DoubleBuffer db;
        private LayoutContext layoutContext;
        private AlarmEventHandler alarm;

        #region Properties

        /// <summary>
        /// Gets or sets the layout context.
        /// </summary>
        /// <value>The layout context.</value>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public LayoutContext LayoutContext
        {
            get { return layoutContext; }
            set { layoutContext = value; }
        }

        /// <summary>
        /// Gets or sets the alarm.
        /// </summary>
        /// <value>The alarm.</value>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public AlarmEventHandler Alarm
        {
            get { return alarm; }
            set { alarm = value; }
        }

        /// <summary>
        /// Gets the buffer.
        /// </summary>
        /// <value>The buffer.</value>
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public DoubleBuffer Buffer
        {
            get { return db; }
        }

        #endregion

        #region Initialze

        /// <summary>
        /// Initializes a new instance of the <see cref="GraphPlotter"/> class.
        /// </summary>
        public GraphPlotter()
        {
            InitializeComponent();
            db = new DoubleBuffer();
            layoutContext = null;
        }

        /// <summary>
        /// Handles the Load event of the GraphPlotter control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void GraphPlotter_Load(object sender, EventArgs e)
        {
            if (db!=null)
                db.CreateOriginalBackground(this.Width, this.Height);
        }

        #endregion

        #region Graphic methods

        /// <summary>
        /// Draws the layout context with specified alarm.
        /// </summary>
        /// <param name="alarm">if set to <c>true</c> then alarm.</param>
        public void Draw(bool alarm)
        {
            this.Draw();
            if (alarm)
                Alarm(this, new AlarmEventArgs(string.Empty));
        }

        /// <summary>
        /// Draws the layout context.
        /// </summary>
        public void Draw()
        {
            if (this.LayoutContext == null) return;
            db.ResetBufferBackground();
            Graphics g = db.GetBufferBackgroundGraphics();
            this.LayoutContext.Draw(g);
            g.Dispose();
            g = this.CreateGraphics();
            db.DrawImage(g, db.GetBufferBackgroundBitmap());
            g.Dispose();
        }

        /// <summary>
        /// Draws the layout context with blind effect.
        /// </summary>
        public void DrawBlind()
        {
            EffectManager.GetDrawingClass(DrawEffect.Blind).Draw(this);
        }

        #endregion

        #region Override methods

        /// <summary>
        /// Raises the <see cref="E:System.Windows.Forms.Control.Paint"></see> event.
        /// </summary>
        /// <param name="e">A <see cref="T:System.Windows.Forms.PaintEventArgs"></see> that contains the event data.</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            Draw();
        }

        /// <summary>
        /// Raises the <see cref="E:Resize"/> event.
        /// </summary>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected override void OnResize(EventArgs e)
        {
            if (db != null)
                db.CreateOriginalBackground(this.Width, this.Height);
        }

        #endregion
    }
}
